package com.it.schoolhomeos.repository;

import com.it.schoolhomeos.entity.Attendance;
import com.it.schoolhomeos.entity.Attendance.AttendanceStatus;
import com.it.schoolhomeos.entity.ClassInfo;
import com.it.schoolhomeos.entity.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

/**
 * 考勤数据访问接口
 * 
 * 提供对Attendance实体的数据库操作方法
 * 继承JpaRepository以获取基本的CRUD操作
 */
@Repository
public interface AttendanceRepository extends JpaRepository<Attendance, Long> {
    
    @Query("SELECT a FROM Attendance a WHERE a.student.id = :studentId")
    List<Attendance> findByStudentId(@Param("studentId") Long studentId);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.id = :studentId")
    Page<Attendance> findByStudentId(@Param("studentId") Long studentId, Pageable pageable);
    
    Page<Attendance> findByDate(LocalDate date, Pageable pageable);
    
    Page<Attendance> findByStatus(AttendanceStatus status, Pageable pageable);
    
    Page<Attendance> findByDateAndStatus(LocalDate date, AttendanceStatus status, Pageable pageable);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.id = :studentId AND a.date = :date AND a.status = :status")
    Page<Attendance> findByStudentIdAndDateAndStatus(
            @Param("studentId") Long studentId, 
            @Param("date") LocalDate date, 
            @Param("status") AttendanceStatus status, 
            Pageable pageable);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.id = :studentId AND a.date = :date")
    Page<Attendance> findByStudentIdAndDate(
            @Param("studentId") Long studentId, 
            @Param("date") LocalDate date, 
            Pageable pageable);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.id = :studentId AND a.status = :status")
    Page<Attendance> findByStudentIdAndStatus(
            @Param("studentId") Long studentId, 
            @Param("status") AttendanceStatus status, 
            Pageable pageable);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.id = :studentId AND a.date BETWEEN :startDate AND :endDate")
    List<Attendance> findByStudentIdAndDateBetween(
            @Param("studentId") Long studentId, 
            @Param("startDate") LocalDate startDate, 
            @Param("endDate") LocalDate endDate);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.id = :studentId AND a.date >= :startDate")
    List<Attendance> findByStudentIdAndDateGreaterThanEqual(
            @Param("studentId") Long studentId, 
            @Param("startDate") LocalDate startDate);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.id = :studentId AND a.date <= :endDate")
    List<Attendance> findByStudentIdAndDateLessThanEqual(
            @Param("studentId") Long studentId, 
            @Param("endDate") LocalDate endDate);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.classInfo = :classInfo")
    Page<Attendance> findByStudentClassInfo(
            @Param("classInfo") ClassInfo classInfo, 
            Pageable pageable);
    
    @Query("SELECT a FROM Attendance a WHERE a.student.classInfo = :classInfo AND a.date = :date")
    Page<Attendance> findByStudentClassInfoAndDate(
            @Param("classInfo") ClassInfo classInfo, 
            @Param("date") LocalDate date, 
            Pageable pageable);
            
    /**
     * 根据班级和日期范围查询考勤记录
     */
    @Query("SELECT a FROM Attendance a WHERE a.student.classInfo = :classInfo AND a.date BETWEEN :startDate AND :endDate")
    List<Attendance> findByStudentClassInfoAndDateBetween(
            @Param("classInfo") ClassInfo classInfo,
            @Param("startDate") LocalDate startDate,
            @Param("endDate") LocalDate endDate);
            
    /**
     * 根据班级和日期查询考勤记录
     */
    @Query("SELECT a FROM Attendance a WHERE a.student.classInfo = :classInfo AND a.date = :date")
    List<Attendance> findByStudentClassInfoAndDateList(
            @Param("classInfo") ClassInfo classInfo,
            @Param("date") LocalDate date);
            
    /**
     * 根据班级、日期和状态查询考勤记录数量
     */
    @Query("SELECT COUNT(a) FROM Attendance a WHERE a.student.classInfo = :classInfo AND a.date = :date AND a.status = :status")
    int countByStudentClassInfoAndDateAndStatus(
            @Param("classInfo") ClassInfo classInfo,
            @Param("date") LocalDate date,
            @Param("status") AttendanceStatus status);
            
    /**
     * 根据学生和日期查询考勤记录
     */
    @Query("SELECT a FROM Attendance a WHERE a.student = :student AND a.date = :date")
    Optional<Attendance> findByStudentAndDate(
            @Param("student") Student student,
            @Param("date") LocalDate date);
            
    /**
     * 根据班级和日期删除考勤记录
     */
    @Query("DELETE FROM Attendance a WHERE a.student.classInfo = :classInfo AND a.date = :date")
    void deleteByStudentClassInfoAndDate(
            @Param("classInfo") ClassInfo classInfo,
            @Param("date") LocalDate date);
} 